package home.rcp.template;

import org.eclipse.jface.action.GroupMarker;
import org.eclipse.jface.action.IContributionItem;
import org.eclipse.jface.action.ICoolBarManager;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.action.ToolBarContributionItem;
import org.eclipse.jface.action.ToolBarManager;
import org.eclipse.swt.SWT;
import org.eclipse.ui.IWorkbenchActionConstants;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.actions.ActionFactory;
import org.eclipse.ui.actions.ActionFactory.IWorkbenchAction;
import org.eclipse.ui.actions.ContributionItemFactory;
import org.eclipse.ui.application.ActionBarAdvisor;
import org.eclipse.ui.application.IActionBarConfigurer;

public class ApplicationActionBarAdvisor extends ActionBarAdvisor {

	private IWorkbenchAction quitAction;
	private IContributionItem perspectiveList;
	private IContributionItem viewList;
	private IWorkbenchAction newWindowAction;
	private IWorkbenchAction savePerspectiveAction;
	private IWorkbenchAction resetPerspectiveAction;
	private IContributionItem openWindowsAction;
	private IWorkbenchAction preferencesAction;

	public ApplicationActionBarAdvisor(IActionBarConfigurer configurer) {
		super(configurer);
	}

	@Override
	protected void makeActions(IWorkbenchWindow window) {
		quitAction = ActionFactory.QUIT.create(window);
		register(quitAction);

		perspectiveList = ContributionItemFactory.PERSPECTIVES_SHORTLIST
				.create(window);

		viewList = ContributionItemFactory.VIEWS_SHORTLIST.create(window);

		newWindowAction = ActionFactory.OPEN_NEW_WINDOW.create(window);
		register(newWindowAction);

		savePerspectiveAction = ActionFactory.SAVE_PERSPECTIVE.create(window);
		register(savePerspectiveAction);

		resetPerspectiveAction = ActionFactory.RESET_PERSPECTIVE.create(window);
		register(resetPerspectiveAction);

		openWindowsAction = ContributionItemFactory.OPEN_WINDOWS.create(window);

		preferencesAction = ActionFactory.PREFERENCES.create(window);
		register(preferencesAction);
	}

	@Override
	protected void fillMenuBar(IMenuManager menuBar) {
		menuBar.add(createFileMenu());
		menuBar.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
		menuBar.add(createWindowMenu());
	}

	private MenuManager createFileMenu() {
		MenuManager menu = new MenuManager("&File",
				IWorkbenchActionConstants.M_FILE);
		menu.add(new GroupMarker(IWorkbenchActionConstants.FILE_START));
		{
			String newText = "&New";
			String newId = ActionFactory.NEW.getId();
			MenuManager newMenu = new MenuManager(newText, newId);
			newMenu.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
			menu.add(newMenu);
		}

		menu.add(new GroupMarker(IWorkbenchActionConstants.NEW_EXT));
		menu.add(new Separator());
		menu.add(new GroupMarker(IWorkbenchActionConstants.SAVE_EXT));
		menu.add(new Separator());
		menu.add(new GroupMarker(IWorkbenchActionConstants.PRINT_EXT));
		menu.add(new Separator());
		menu.add(new GroupMarker(IWorkbenchActionConstants.OPEN_EXT));
		menu.add(new Separator());
		menu.add(new GroupMarker(IWorkbenchActionConstants.IMPORT_EXT));
		menu.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
		menu.add(new Separator());
		menu.add(quitAction);
		menu.add(new GroupMarker(IWorkbenchActionConstants.FILE_END));
		return menu;
	}

	private MenuManager createWindowMenu() {
		MenuManager menu = new MenuManager("&Window",
				IWorkbenchActionConstants.M_WINDOW);

		menu.add(newWindowAction);

		menu.add(new Separator());
		MenuManager openPerspectiveMenu = new MenuManager("&Open Perspective",
				"openPerspective");
		openPerspectiveMenu.add(perspectiveList);
		menu.add(openPerspectiveMenu);

		MenuManager showViewMenu = new MenuManager("Show &View", "showView");
		showViewMenu.add(viewList);
		menu.add(showViewMenu);

		menu.add(new Separator());
		menu.add(savePerspectiveAction);
		menu.add(resetPerspectiveAction);

		menu.add(openWindowsAction);
		menu.add(new Separator());

		menu.add(preferencesAction);

		return menu;
	}

	@Override
	protected void fillCoolBar(ICoolBarManager coolBar) {
		coolBar.add(new GroupMarker("group.file")); //$NON-NLS-1$ 
		IToolBarManager fileToolBar = new ToolBarManager(SWT.FLAT | SWT.RIGHT);
		fileToolBar.add(new Separator(IWorkbenchActionConstants.NEW_GROUP));
		fileToolBar.add(new GroupMarker(IWorkbenchActionConstants.NEW_EXT));
		fileToolBar.add(new GroupMarker(IWorkbenchActionConstants.SAVE_GROUP));
		fileToolBar.add(new GroupMarker(IWorkbenchActionConstants.SAVE_EXT));
		fileToolBar.add(new GroupMarker(IWorkbenchActionConstants.PRINT_EXT));
		fileToolBar.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));

		coolBar.add(new ToolBarContributionItem(fileToolBar,
				IWorkbenchActionConstants.TOOLBAR_FILE));
	}

}
